package rs.mbs2.epics.server.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import rs.mbs2.epics.server.entity.Album;
import rs.mbs2.epics.server.entity.Photo;
import rs.mbs2.epics.server.entity.User;
import rs.mbs2.epics.server.servlet.util.FileManager;
import rs.mbs2.epics.server.session.dao.localinterface.AlbumDaoLocal;
import rs.mbs2.epics.server.session.dao.localinterface.PhotoDaoLocal;

@SuppressWarnings("serial")
public class DeletePhotoController extends HttpServlet {

	private static Logger log = Logger.getLogger(DeletePhotoController.class);

	@EJB
	private AlbumDaoLocal albumDao;

	@EJB
	private PhotoDaoLocal photoDao;

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String photoId = request.getParameter("id");
		Integer id = Integer.parseInt(photoId);
		Photo photo = photoDao.findById(id);
		

		List<Album> albums = albumDao.findAlbumsByPhotoId(id);
		for(Album a : albums) {
			a.getPhotos().remove(photo);
			albumDao.merge(a);
		}
		
		User user = (User) request.getSession().getAttribute("user");		

		photoDao.remove(photo);
		

		
		String photoLocation = photo.getLocation();
		String thumbnailLocation = photo.getThumbnailLocation();
//		System.out.println("Photo location: " + photoLocation);
		int cntOriginal = photoLocation.lastIndexOf("/");
		int cntThumb = thumbnailLocation.lastIndexOf("/");
		String originalName = photoLocation.substring(cntOriginal+1);
		String originalThumbName = thumbnailLocation.substring(cntThumb+1);
//		System.out.println("Original name: "+originalName);
//		System.out.println("Thumbnail name: "+originalThumbName);
		
		
		
		int index = 0;
		if(albums.get(index).getTitle().equalsIgnoreCase("favorites"))
			index = 1;
		
		
		String photoFilePath = getServletContext().getRealPath("")
				+ "\\Files\\" + user.getId() + "\\" + albums.get(index).getId() + "\\" + originalName;
//		System.out.println("PUTANJA ZA BRISANJE: " + photoFilePath);
		File photoToDelete = new File(photoFilePath);
		FileManager.delete(photoToDelete);
		
		String thumbFilePath = getServletContext().getRealPath("")
				+ "\\Files\\" + user.getId() + "\\" + albums.get(index).getId() + "\\thumbnails\\" + originalThumbName;
//		System.out.println("PUTANJA ZA BRISANJE THUMB: " + thumbFilePath);

		File thumbToDelete = new File(thumbFilePath);
		FileManager.delete(thumbToDelete);
		
		
		
		
		log.info(new Date() + " : obrisana slika:" + photo.getId());
		response.sendRedirect("./ViewAlbumController?id=" + albums.get(0).getId());

	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
